import 'virtual:uno.css';
import "/@/design/index.less"
import "ant-design-vue/dist/reset.css"

import loadjs from "loadjs"

import App from "./App.vue"
import { createApp } from "vue"
import * as cache from "/@/utils/cache/module"
import { initAppConfigStore } from "/@/logics/initAppConfig"
import { setupErrorHandle } from "/@/logics/error-handle"
import { router, setupRouter } from "/@/router"
import { setupRouterGuard } from "/@/router/guard"
import { setupStore } from "/@/store"
import { setupGlobDirectives } from "/@/directives"
import { setupI18n } from "/@/locales/setupI18n"
import { registerGlobComp } from "/@/components/registerGlobComp"
import { registerThirdComp } from "/@/settings/registerThirdComp"
import { useIam } from "/@/hooks/web/useIam"
import { registerPackages } from "/@/utils/monorepo/registerPackages"
import { useIamStoreWithOut } from "/@/store/modules/iam"
import { Modal } from "ant-design-vue"
import { queryAllDictItems } from "/@/api/infra/dict"

async function clearCache() {
  const res = await queryAllDictItems()
  cache.removeDictData()
  cache.setDictItems(res.data)
}

const iam = useIam({
  loginTimoutCallback: (url) => {
    Modal.confirm({
      content: "登录态已过期，请重新登录",
      iconType: "warning",
      closable: false,
      visible: true,
      cancelButtonProps: { style: { display: "none" } }, // 隐藏取消按钮
      okText: "重新登录",
      cancelText: "取消",
      onOk: () => {
        setTimeout(() => {
          window.location.href = url
        }, 200)
      }
    })
  },
  reLoginCallback: (message) => {
    Modal.confirm({
      content: message,
      iconType: "warning",
      closable: false,
      visible: true,
      okText: "刷新页面",
      onOk: () => {
        setTimeout(() => {
          window.location.href = window.location.protocol + "//" + window.location.host
        }, 200)
      }
    })
  }
})

async function bootstrap() {
  // 创建应用实例
  const app = createApp(App)

  // 多语言配置,异步情况:语言文件可以从服务器端获得
  await setupI18n(app)

  // 配置存储
  setupStore(app)

  // 初始化内部系统配置
  initAppConfigStore()

  //CAS单点登录
  await iam.init()

  // 初始化应用配置
  const appConfig = await useIamStoreWithOut().initAppPublicConfig()
  window.document.title = appConfig.name

  // 注册外部模块路由
  registerPackages(app)

  // 注册全局组件
  registerGlobComp(app)

  // 配置路由
  setupRouter(app)

  // 路由保护
  setupRouterGuard(router)

  // 注册全局指令
  setupGlobDirectives(app)

  // 配置全局错误处理
  setupErrorHandle(app)

  clearCache()

  // 注册第三方组件
  await registerThirdComp(app)

  // 当路由准备好时再执行挂载( https://next.router.vuejs.org/api/#isready)
  await router.isReady()

  // 挂载应用
  app.mount("#app", true)
}

bootstrap()
